/*
 ============================================================================
 Name		: FbInformation.cpp
 Author	  : HeQun
 Version	 : 1.0
 Copyright   : Copyright.2009
 Description : CFbInformation implementation
 ============================================================================
 */

#include <Stringloader.h>
#include <EIKENV.H>

#include "FbInformation.h"
#include "flashbox4.mbg"
#include "ConstDefined.hrh"
#include "Flashbox4_0x20021B21.rsg"
#include "FbUtils.h"
#include <BAUTILS.H>

CFbInformation::CFbInformation():m_IsShowDiskInfo(EFalse)
{
	// No implementation required
}

CFbInformation::~CFbInformation()
{
	delete m_TitleBuf;
	delete m_ContentBuf;
	delete m_pAppLogoBmp;
}

CFbInformation* CFbInformation::NewLC(TInt aTitleResId, const TDesC& aFilename, const TRect& aRect, CCoeControl& aParent)
{
	CFbInformation* self = new (ELeave) CFbInformation();
	CleanupStack::PushL(self);
	self->ConstructL(aTitleResId, aFilename, aRect, aParent);
	return self;
}

CFbInformation* CFbInformation::NewL(TInt aTitleResId, const TDesC& aFilename, const TRect& aRect, CCoeControl& aParent)
{
	CFbInformation* self = CFbInformation::NewLC(aTitleResId, aFilename, aRect, aParent);
	CleanupStack::Pop(); // self;
	return self;
}

void CFbInformation::ConstructL(TInt aTitleResId, const TDesC& aFilename, const TRect& aRect, CCoeControl& aParent)
{
	SetContainerWindowL(aParent);
	
	m_pAppLogoBmp = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
				EMbmFlashbox4Applogo );
	
	LoadStringsFromResourceL(aTitleResId, aFilename);
	
	if(aTitleResId == R_INFO_ABOUT_TITLE)
	{
		m_IsShowDiskInfo = ETrue;
	}
	
	SetRect(aRect);
	
	ActivateL();
}

void CFbInformation::SizeChanged()
{
	DrawNow();
}

TInt CFbInformation::CountComponentControls() const
{
	return 0;
}

CCoeControl* CFbInformation::ComponentControl(TInt aIndex) const
{
	switch(aIndex)
	{
	default:
		return NULL;
	}
}

void CFbInformation::Draw(const TRect& /*aRect*/) const
{
	CWindowGc& gc = SystemGc();
	
	TRect InfoRect = TRect(TPoint(Rect().iTl.iX, Rect().iTl.iY + 50), TPoint(Rect().iBr));
	
	gc.SetBrushColor(KFbTabgroupTopColor);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(TRect(41, 0, 240, 30));
	
	gc.SetBrushColor(KFbSettingTitleRectColor);
	gc.DrawRect(KFbSettingTitleRect);
	
	gc.SetBrushColor (KFbSettingTopRect1Color);
	gc.DrawRect(InfoRect);
	
	if (m_pAppLogoBmp)
	{
		gc.DrawBitmap(KFbAppLogoRect, m_pAppLogoBmp);
	}

	//-------------Draw Title & Content---------<BEGIN>
	CFont* font = NULL;//iCoeEnv->NormalFont();
	TFontSpec fontsp = iCoeEnv->NormalFont()->FontSpecInTwips();
	TInt font_oHeight = fontsp.iHeight;
	fontsp.iHeight = 13 * font_oHeight / 20;
	iCoeEnv->ScreenDevice()->GetNearestFontInTwips(font, fontsp);
	TInt fontHeightPixel = font->HeightInPixels();
	if (font)
		gc.UseFont(font);
	else
		gc.UseFont(iCoeEnv->NormalFont());
	
	gc.SetPenStyle(CGraphicsContext::ESolidPen);
	if(m_TitleBuf)
	{
		gc.SetPenColor(KFbSettingTitleTextColor);//KRgbBlack
		gc.DrawText(m_TitleBuf->Des(), TPoint(44,47));//KFbSettingTitlePoint
	}
	if(m_ContentBuf)
	{
		gc.SetPenColor(KRgbBlack);
		TPtr textptr = m_ContentBuf->Des();
		TInt iPos = 0;
		TInt i = 0;		
		textptr = m_ContentBuf->Des().RightTPtr(m_ContentBuf->Length()-1);
		while(((iPos = textptr.Find(_L("\r\n"))) != KErrNotFound))
		{
			TBuf<50> ttb;
			ttb.SetLength(iPos+1);
			ttb.Delete(0, iPos+1);
			ttb.Copy(textptr.LeftTPtr(iPos));
			textptr = textptr.RightTPtr(textptr.Length()-iPos-2);
			gc.DrawText(ttb, TPoint(2,70 + fontHeightPixel*i));
			i++;
		}
	}
	
	//---------Display Disk Infomation----------<Begin>//
	if(m_IsShowDiskInfo)
	{
		RFs fs;
		User::LeaveIfError(fs.Connect());
		TVolumeInfo i_volInfo;
		fs.Volume(i_volInfo);
		TBuf<40> diskFreeBuf;
		HBufC* tmpBuf = StringLoader::LoadL(R_DISK_FREE_INFO);
		diskFreeBuf.Copy(*tmpBuf);
		diskFreeBuf.AppendFormat(_L(" %d MB"), i_volInfo.iFree/1024/1024);
		gc.DrawText(diskFreeBuf, TPoint(2,260));
		if(tmpBuf)
			delete tmpBuf;
		tmpBuf = StringLoader::LoadL(R_DISK_TOTAL_INFO);
		diskFreeBuf.Copy(*tmpBuf);
		diskFreeBuf.AppendFormat(_L(" %d MB"), i_volInfo.iSize/1024/1024);
		gc.DrawText(diskFreeBuf, TPoint(2,280));
		delete tmpBuf;
		fs.Close();
	}
	//---------Display Disk Infomation----------<End>//
	
	gc.DiscardFont();
	iCoeEnv->ScreenDevice()->ReleaseFont(font);
	//-------------Draw Title & Content---------<END>
	
}

void CFbInformation::LoadStringsFromResourceL(TInt aTitleResId, const TDesC& aFilename)
{
	if(m_TitleBuf)
	{
		delete m_TitleBuf;
	}
	if(aTitleResId <= 0)
		return;
	
	m_TitleBuf = StringLoader::LoadL(aTitleResId);
	
	if(m_ContentBuf)
	{
		delete m_ContentBuf;
	}
	
	
	RFs fs;
	User::LeaveIfError(fs.Connect());
	if (!BaflUtils::FileExists (fs, aFilename))
	{
		fs.Close();
		return;
	}
	m_ContentBuf = ReadUnicodeFileL(fs, aFilename);
	fs.Close();
	//m_ContentBuf = StringLoader::LoadL(aContentResId);	
}

void CFbInformation::SetTitleStringsL(const TDesC& aTitle)
{
	if(aTitle.Length() > 0)
	{
		if (m_TitleBuf != NULL)
		{
			delete m_TitleBuf;
		}
		
		m_TitleBuf = aTitle.AllocL();
	}
		
}

void CFbInformation::SetContentStringsL(const TDesC& aContent)
{
	if(aContent.Length() > 0)
	{
		if (m_ContentBuf != NULL)
		{
			delete m_ContentBuf;
		}
		
		m_ContentBuf = aContent.AllocL();
	}
	
}

TKeyResponse CFbInformation::OfferKeyEventL (const TKeyEvent &aKeyEvent, TEventCode aType )
{
	switch (aKeyEvent.iCode)
		{
		case EKeyUpArrow://上拨
			
			break;
		case EKeyDownArrow://下拨
			
			break;		
		default:
			return EKeyWasNotConsumed;
		}
	return EKeyWasNotConsumed;//EKeyWasConsumed
}
/*
TDes CFbInformation::EnCodeString(TDes& inStr)
{
	TInt ln = inStr.Length();
	TBuf<ln> tmpStr;
	tmpStr = inStr.AllocL();
}
*/
